/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package stateless;

import java.sql.*;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;

/**
 *
 * @author DELL
 */
@Stateful
public class CartBean implements CartBeanLocal {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/bookstore?useUnicode=true&characterEncoding=utf-8";
    static final String USER = "root";
    static final String PASS = "root";
    
    private Connection conn;
    private Statement stmt;
    private ArrayList<ArrayList<String>> goods=new ArrayList<>();
    
    public CartBean(){ }
    
    @PostConstruct
    @PostActivate
    public void openConnection(){
        try{
            Class.forName(JDBC_DRIVER);                             // 注册 JDBC 驱动
            conn = DriverManager.getConnection(DB_URL, USER, PASS); // 打开链接
            stmt = conn.createStatement();                          //构造查询驱动
        }
        catch (SQLException se) {                   // 处理 JDBC 错误 
            System.out.println("BookBean openCoonenction raise() Exception:"+se.getMessage());
        }
        catch (ClassNotFoundException e) {          // 处理 Class.forName 错误
            System.out.println("BookBean openCoonenction raise() Exception"+e.getMessage());
        }      
    }
    
    @PrePassivate
    @PreDestroy
    public void cleanup(){
        try {   
            if (stmt != null) stmt.close();
        }catch (SQLException se) { System.out.println("BookBean cleanup() raise Exception"+se.getMessage());}
        try {
            if (conn != null) conn.close();
        }catch (SQLException se) {System.out.println("BookBean cleanup() raise Exception"+se.getMessage()); }
    }

    @Override
    public void addGood(int id, int number) {
        ArrayList<String> good = new ArrayList<>();
        for(ArrayList<String> g:goods){
            if(Integer.valueOf(g.get(0))==id){
                g.set(1,String.valueOf((Integer.valueOf(g.get(1))+number)));
                return;
            }
        }
        good.add(String.valueOf(id));
        good.add(String.valueOf(number));
        this.goods.add(good);
        /*String sql = String.format("insert into cart values (%d,%d)",id,number) ;
        try {
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            System.out.println("CartBean addGood raise Exception"+ex.getMessage());
        }*/
    }

    @Override
    public ArrayList<ArrayList<String>> getGoods() {
        return this.goods;
        /*String sql = "select * from cart" ;
        ArrayList<ArrayList<String>> result = new ArrayList<>();
        try {
            ResultSet rs= stmt.executeQuery(sql);
            while(rs.next()){
                ArrayList<String> list = new ArrayList<>();
                for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++){
                    list.add(rs.getString(i));
                }
                result.add(list);
            }
        } catch (SQLException ex) {
            System.out.println("CartBean getGoods() raise Exception"+ex.getMessage());
        }
        return result;*/
    }   

    @Override
    public void cleanCart() {
        this.goods.clear();
        /*String sql = "truncate table cart" ;
        try{
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.executeUpdate();
        }catch (SQLException ex) {
            System.out.println("cleanCart() raise Exception"+ex.getMessage());
        }*/
    }
    
}
